<template>
  <ul
    class="base-tab flex"
    :class="[
      `base-tab--${props.type}`,
      props.disabled ? 'base-tab--disabled' : '',
    ]"
  >
    <li
      class="flex-1 opacity radius click click--bcl"
      v-for="(v, i) in props.tabs"
      :class="i === props.index ? 'active' : ''"
      :key="i"
      @click="onTabChange(i)"
    >
      {{ v.name }}
    </li>
  </ul>
</template>

<script setup>
import selects from 'js/selects'

const props = defineProps({
  tabs: {
    type: Array,
    default: () => selects.productTypes,
  },

  index: {
    type: Number,
    default: 0,
  },

  value: {
    type: [String, Number],
    default: 0,
  },

  key: {
    type: String,
    default: '',
  },

  type: {
    type: Number,
    default: 1,
  },

  disabled: {
    type: Boolean,
    default: false,
  },
})
const emit = defineEmits([
  'change',
  'update:index',
  'update:value',
  'update:key',
])

if (!props.disabled) {
  onTabChange(props.index, false)
}

function onTabChange(i, isChange = true) {
  const v = props.tabs[i]

  emit('update:index', i)
  emit('update:value', v.value)
  emit('update:key', v.key)

  isChange &&
    emit('change', {
      index: i,
      ...v,
    })
}
</script>

<style lang="scss" scoped>
.base-tab {
  li {
    position: relative;
    color: var(--color-black);
    text-align: center;
    height: 35px;
    line-height: 35px;

    &:not(:last-child) {
      margin-right: 5px;
    }

    &.active {
      font-weight: bolder;

      &::after {
        content: '';
        position: absolute;
        left: 50%;
        bottom: 0;
        width: 30%;
        height: 2px;
        background-color: var(--color-black);
        transform: translateX(-50%);
      }
    }
  }

  &--2 {
    li {
      background-color: rgba($color: #7262b1, $alpha: 0.4) !important;
      color: var(--color-white);
      border: 1px solid #7477b3;

      &.active {
        font-weight: bolder;
        background-color: rgba($color: #7262b1, $alpha: 0.8) !important;

        &::after {
          display: none;
        }
      }
    }
  }

  &--disabled {
    li {
      background-color: rgba($color: #7262b1, $alpha: 0.8) !important;
    }
  }
}
</style>
